/**
 * Created by djh on 2016/11/17.
 * 资讯js
 *
 */

define(function (require, exports) {
    var common = require("common");
    var template = require("template");
    var order_car = require("order_car");

    exports.index=function(){
        common.topMenu(1);
        //筛选，更多
        $(".summary").on("click", ".more", function () {
            var that = $(this);
            if (that.hasClass("active")) {
                that.siblings(".cont").css("height", "24px");
            } else {
                that.siblings(".cont").css("height", "auto");
                //that.siblings(".cont").find("a").that.val()
            }
            $(this).toggleClass("active");
        });
        //关闭筛选结果
        $(".result").on("click",".crumb_c",function(){
            var par = $(this).parent();  
              //  par.hide();
              if(par.hasClass("a")){
                $('.aa').trigger("click");
              }else if(par.hasClass("b")) {
                $('.bb').trigger("click");
              }else if (par.hasClass("c")) {
                $('.cc').trigger("click");
              }else if (par.hasClass("d")) {
                $('.dd').trigger("click");
              }


        });


        //商品列表 分类切换
        $(".pro_list").on("click", ".tab li", function () {
            var $this = $(this);
            if ($this.hasClass("active")) {
                $(this).toggleClass("down");
            }
            $this.addClass("active").siblings(".active").removeClass("active");

            if ($this.hasClass("down")) {

                $('input[name=order_way]').val('desc');
            }else{

                $('input[name=order_way]').val('asc');
            }

            $('input[name=order_name]').val($this.data('ordername'));

            $("#form").submit();
        });
        //頁碼輸入限制
        $(".pageBar2 .pageInput").on("blur",function(){
            var $this = $(this);
            var max = $this.data("max");
            var val = $this.val();
            var result = /^[1-9]+\d*$/.test(val);
            if(!result) {
                layer.tips("请输入正确的页码数字",$this,{tips:3});
                return;
            }else if(val<1){
                $this.val(1);
            }else if(val>max) {
                $this.val(max);
            }

        });
        //输入页码跳页
        $(".js-jump-page").click(function () {
            $('input[name=page]').val($('input[name=tmp_page]').val());
            $("#form").submit();
        });

        $(".summary").on("click", "[data-act='search']",function () {

            if($(this).data('name')=='type_name'){
                $('input[name=type_name]').val($(this).data('value'));
            }
            if($(this).data('name')=='product_name'){

                $('input[name=product_name]').val($(this).data('value'));
            }
            if($(this).data('name')=='repertory_city_name'){

                $('input[name=repertory_city_name]').val($(this).data('value'));
            }
            if($(this).data('name')=='delivery_way'){

                $('input[name=delivery_way]').val($(this).data('value'));
            }

            $("#form").submit();
        });
    };
    exports.buy = function (username) {
        //添加到购物车
        $(".pro_list").on("click", "[data-act='add_car']", function () {
            /*if(username=="") {
                location.href="/member/login/";
                layer.open({
                    title:"温馨提示",
                    btn:["登录","注册"],
                    content:'<div class="c-danger">您还没登录，不能进行此项操作，请先登录或者注册</div> ',
                    yes:function(){
                        location.href="/member/login/"
                    },btn2:function(){
                        location.href="/member/reg/"
                    }
                });
                return ;
            }*/
            var id = $(this).parents('tr').attr('data-id');

            var car_num = $(".carNum_id").html();
            if (car_num == "") {
                car_num = 0;
            }
            common.ajaxHandle({
                url: "/trading/product/addCartProduct",
                data: {id: id},
                callback: function () {
                    layer.msg("成功添加到购物车");
                    car_num++;
                    $(".carNum_id").addClass("badge").html(car_num);
                }
            });
        });
        //直接购买
        $(".pro_list").on("click", "[data-act='buy']", function () {
            if(username=="") {
                location.href="/member/login/";
               /* layer.open({
                    title:"温馨提示",
                    btn:["登录","注册"],
                    content:'<div class="c-danger">您还没登录，不能进行此项操作，请先登录或者注册</div> ',
                    yes:function(){
                        location.href="/member/login/"
                    },btn2:function(){
                        location.href="/member/reg/"
                    }
                });*/
                return ;
            }
            var id = $(this).parents('tr').attr('data-id');

            common.ajaxHandle({
                url: "/trading/product/getProduct",
                data: {id: id},
                callback: function (data) {
                    data.data.product_actual_price = data.data.min_volume * data.data.sales_price;
                    var temp = template("buyTemp", data.data);//{'productName':'PVC'}
                    layer.open({
                        type: 1,
                        title: "请确认您的商品信息",
                        btn: ["确认购买", "取消"],
                        area: ["800px", "300px"],
                        content: temp,
                        success: function () {
                            order_car.numBox();
                        }, yes: function (index, layero) {
                            layer.load();
                            layero.find(".formLayer").submit();
                        }
                    })
                }
            });


        });

    };


});

